﻿namespace Shanda.DevFx.Security.Web.Pages
{
    using Shanda.DevFx.Core;
    using Shanda.DevFx.Security;
    using Shanda.DevFx.Security.Web;
    using System;
    using System.Collections;
    using System.Web.UI.WebControls;

    public class UserPage : Shanda.DevFx.Security.Web.BasePage
    {
        protected Button btnSearch;
        protected Button btnShowAll;
        protected Label lblMessage;
        private bool m_bShowAll = true;
        protected Shanda.DevFx.Security.Web.Pages.PagerControl pager;
        protected Panel Panel1;
        protected Repeater repeater;
        protected TextBox tbUserName;

        private void BindData(int pageIndex)
        {
            User[] userArray;
            if (this.m_bShowAll)
            {
                userArray = Securitor.UserManager.ListItem();
            }
            else
            {
                userArray = Securitor.UserManager.FindUser(this.FindUserName);
            }
            this.pager.ItemCount = userArray.Length;
            this.pager.PageIndex = pageIndex;
            ArrayList list = new ArrayList(userArray);
            User[] array = new User[this.pager.ItemLength];
            list.CopyTo(this.pager.StartIndex, array, 0, this.pager.ItemLength);
            this.pager.DataSource = array;
            this.pager.DataBind();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (this.FindUserName == string.Empty)
            {
                this.m_bShowAll = true;
                this.BindData(0);
            }
            else
            {
                this.m_bShowAll = false;
                this.BindData(0);
            }
        }

        private void btnShowAll_Click(object sender, EventArgs e)
        {
            this.m_bShowAll = true;
            this.BindData(0);
        }

        private void InitializeComponent()
        {
            this.pager.PageIndexChanged += new Shanda.DevFx.Security.Web.Pages.PagerPageEventHandler(this.pager_PageIndexChanged);
            this.btnSearch.add_Click(new EventHandler(this.btnSearch_Click));
            this.btnShowAll.add_Click(new EventHandler(this.btnShowAll_Click));
            this.repeater.add_ItemCreated(new RepeaterItemEventHandler(this, (IntPtr) this.repeater_ItemCreated));
            this.repeater.add_ItemCommand(new RepeaterCommandEventHandler(this, (IntPtr) this.repeater_ItemCommand));
            base.add_Load(new EventHandler(this.Page_Load));
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.get_IsPostBack())
            {
                this.BindData(0);
            }
        }

        private void pager_PageIndexChanged(object sender, Shanda.DevFx.Security.Web.Pages.PagerPageEventArgs e)
        {
            this.BindData(e.NewPageIndex);
        }

        private void repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            IAOPResult result = null;
            if (e.get_CommandName() == "DELETE")
            {
                result = Securitor.UserManager.DeleteItem((string) e.get_CommandArgument());
            }
            if (result != null)
            {
                if (result.IsSuccess)
                {
                    this.lblMessage.set_Text("操作成功");
                    this.BindData(this.pager.PageIndex);
                }
                else
                {
                    this.lblMessage.set_Text(result.ResultDescription);
                }
            }
        }

        private void repeater_ItemCreated(object sender, RepeaterItemEventArgs e)
        {
            LinkButton button = e.get_Item().FindControl("btnDelete") as LinkButton;
            if (button != null)
            {
                button.get_Attributes().set_Item("onclick", "return confirm('确认删除？')");
            }
        }

        protected string FindUserName
        {
            get
            {
                return this.tbUserName.get_Text().Trim();
            }
        }
    }
}

